// pages/bill/bill.js
import api from '../../utils/api'
import { uncollectedQuery, propertyQuery, uncollectedAccount } from '../../utils/conf'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dialog: false,
    houses: [],
    house_id: 0,
    house_name: '',
    meter_time: '',
    list: []
  },
  close: function () {
    this.setData({
      dialog: false,
    })
  },
  openIOS1: function () {
    this.setData({
      dialog: true
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryHouse()
    this.query()
  },

  queryHouse() {
    let that = this
    api.get(propertyQuery)
      .then(res => {
        // console.log(res)
        let data = res.data
        that.setData({
          houses: data,
          house_name: that.selectHouse(data, res.msg),
          house_id: res.msg
        })
      }).catch(err => {
        console.log(err)
      })
  },

  selectHouse(arrayList, house_id) {
    let house_name = ''
    for (let index = 0; index < arrayList.length; index++) {
      if (arrayList[index].id == house_id) {
        house_name = arrayList[index].name
        break
      }
    }
    return house_name
  },

  query(house_id = 0) {
    let that = this
    if (house_id) {
      api.post(uncollectedQuery, {
        house_property_id: house_id,
        meter_reading_time: that.data.meter_time
      })
        .then(res => {
          // console.log(res)
          that.setData({
            list: res.data
          })
        }).catch(err => {
          console.log(err)
        })
    } else {
      api.get(uncollectedQuery)
        .then(res => {
          // console.log(res)
          that.setData({
            list: res.data
          })
        }).catch(err => {
          console.log(err)
        })
    }
  },

  onShowTap(e) {
    let { index } = e.currentTarget.dataset
    // console.log(this.data.list[index].id)
    let id = this.data.list[index].id
    wx.navigateTo({
      url: '/pages/bill/show?id=' + id
    })
  },

  houseChange(e) {
    // console.log(e.target.dataset.id)
    this.setData({
      house_name: this.selectHouse(this.data.houses, e.target.dataset.id),
      meter_time: '',
      house_id: e.target.dataset.id
    })
    this.query(e.target.dataset.id)
    this.close()
  },

  // 日期选择框
  bindDateChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      meter_time: e.detail.value
    })
    this.query(this.data.house_id)
  },

  onModifyTap(e) {
    let { index } = e.currentTarget.dataset
    // console.log(this.data.list[index].id)
    let id = this.data.list[index].id
    wx.navigateTo({
      url: '/pages/bill/modify?id=' + id
    })
  },

  onAccountTap(e) {
    let { index } = e.currentTarget.dataset
    let that = this
    // console.log(this.data.list[index])
    let { name, total_money, id } = this.data.list[index]
    wx.showModal({
      title: '提示',
      content: name + '的' + total_money + '元已到账？',
      // confirmText: '确定',
      success: ({ confirm }) => {
        if (confirm) {
          api.post(uncollectedAccount, {
            id: id
          })
            .then(res => {
              // console.log(res)
              wx.showToast({
                title: res.msg,
                icon: 'success',
                duration: 1000,
                success: function () {
                  setTimeout(() => {
                    that.query()
                  }, 1000);
                }
              })
            }).catch(err => {
              console.log(err)
              wx.showToast({
                title: err.msg,
                icon: 'error',
                duration: 1000
              })
            })
        }
      },
    })
  },
})